package net.homeftp.heimo.weddingwidget.perf;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.homeftp.heimo.weddingwidget.BuildConfig;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class BasicPreferenceManager {
	private static final String TAG = "BasicPreferenceManager";
	protected static final String PREFS_NAME = "HeaWeddingWidgetPrefs";
	protected static SimpleDateFormat smf = null;
	protected SharedPreferences sPref = null;
	protected Context ctx = null;
	protected boolean widgetConfigMode = false;
	protected int appId = 0;

	public Object fnGetPref(String key, Object defaultObj) {
		String widgetId = key + appId;
		if (defaultObj instanceof String) {
			String defVal = defaultObj.toString();
			String retVal = defVal;
			if (widgetConfigMode == true) {
				retVal = sPref.getString(widgetId, defVal);
				if (retVal == defVal) {
					retVal = sPref.getString(key, defVal);
					log("[fnGetPref] appID : " + key + "||" + retVal);
				} else {
					log("[fnGetPref] widID : " + widgetId + "||" + retVal);
				}
			} else {
				retVal = sPref.getString(key, defVal);
				log("[fnGetPref] appID : " + key + "||" + retVal);
			}
			return retVal;
		} else if (defaultObj instanceof Integer) {
			int defVal = ((Integer) defaultObj).intValue();
			int retVal = defVal;
			if (widgetConfigMode == true) {
				retVal = sPref.getInt(widgetId, defVal);
				if (retVal == defVal) {
					retVal = sPref.getInt(key, defVal);
					log("[fnGetPref] appID : " + key + "||" + retVal);
				} else {
					log("[fnGetPref] widID : " + widgetId + "||" + retVal);
				}
			} else {
				retVal = sPref.getInt(key, defVal);
				log("[fnGetPref] appID : " + key + "||" + retVal);
			}
			return retVal;
		} else if (defaultObj instanceof Long) {
			long defVal = ((Long) defaultObj).longValue();
			long retVal = defVal;
			if (widgetConfigMode == true) {
				retVal = sPref.getLong(widgetId, defVal);
				if (retVal == defVal) {
					retVal = sPref.getLong(key, defVal);
					log("[fnGetPref] appID : " + key + "||" + retVal);
				} else {
					log("[fnGetPref] widID : " + widgetId + "||" + retVal);
				}
			} else {
				retVal = sPref.getLong(key, defVal);
				log("[fnGetPref] appID : " + key + "||" + retVal);
			}
			return retVal;
		} else if (defaultObj instanceof Boolean) {
			boolean defVal = ((Boolean) defaultObj).booleanValue();
			boolean retVal = defVal;
			if (widgetConfigMode == true) {
				retVal = sPref.getBoolean(widgetId, defVal);
				if (retVal == defVal) {
					retVal = sPref.getBoolean(key, defVal);
					log("[fnGetPref] appID : " + key + "||" + retVal);
				} else {
					log("[fnGetPref] widID : " + widgetId + "||" + retVal);
				}
			} else {
				retVal = sPref.getBoolean(key, defVal);
				log("[fnGetPref] appID : " + key + "||" + retVal);
			}
			return retVal;
		} else {
			return null;
		}
	}

	public void fnSavePref(String key, Object value) {
		if (widgetConfigMode == true) {
			key += appId;
		}
		log("[fnSavePref] widID : " + key + "||" + value);
		SharedPreferences.Editor editor = sPref.edit();
		if (value instanceof String) {
			editor.putString(key, value.toString());
		} else if (value instanceof Date) {
			String sDt = smf.format(value);
			editor.putString(key, sDt);
		} else if (value instanceof Integer) {
			editor.putInt(key, ((Integer) value).intValue());
		} else if (value instanceof Long) {
			editor.putLong(key, ((Long) value).longValue());
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, ((Boolean) value).booleanValue());
		}
		editor.commit();
	}

	public SimpleDateFormat getDateFormatter() {
		if (smf == null) {
			smf = new SimpleDateFormat("yyyy.MM.dd");
		}
		return smf;
	}

	public int getAppId() {
		return this.appId;
	}

	private void log(String msg) {
		if (BuildConfig.DEBUG) {
			Log.d(TAG, msg);
		}
	}
}
